//
//  AddBBSController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/21.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "AddBBSController.h"
#import "PhotoController.h"
@interface AddBBSController ()
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;
@property (weak, nonatomic) IBOutlet UIButton *addImgBtn;
@property (weak, nonatomic) IBOutlet UIImageView *showImg;
@property (strong, nonatomic) NSString *imgUrl;
@end

@implementation AddBBSController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"发帖";
    [self.contentTextView becomeFirstResponder];
    [self addRightNavBar];
}
- (void)addRightNavBar
{
    UIButton* _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnRight setTitle:@"发送" forState:UIControlStateNormal];
    [_btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btnRight setBounds:CGRectMake(0, 0, 50, 44)];
    UIBarButtonItem* Rightitem  = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
    [_btnRight addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = Rightitem;
}
- (void)rightButtonAction

{
    if ([OpenLoginManager openLoginPageWithController:self]) {
        return;
    }
    
    if (self.contentTextView.text.length == 0) {
        [EasyTextView showText:@"请输入要发布文字"];
        return;
    }
    if (self.imgUrl.length == 0) {
        [EasyTextView showText:@"请选择一张图片"];
        return;
    }
    BmobUser *buser = [BmobUser currentUser];
    
    BmobObject *object = [[BmobObject alloc] initWithClassName:@"Articles"];
    [object setObject:@"0" forKey:@"commentCount"];
    [object setObject:self.contentTextView.text forKey:@"content"];
    [object setObject:[buser objectForKey:@"headImg"] forKey:@"headImgUrl"];
    [object setObject:@"0" forKey:@"likeCount"];
    [object setObject:[buser objectForKey:@"displayName"] forKey:@"name"];
    [object setObject:self.imgUrl forKey:@"showImg"];
    [object setObject:buser.objectId forKey:@"userID"];
    [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        [EasyTextView showSuccessText:@"发布成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (IBAction)tapViewClickAction:(UITapGestureRecognizer *)sender {
    if (self.contentTextView.isFirstResponder) {
         [self.contentTextView resignFirstResponder];
    }
    
    
}
- (IBAction)addImgBtnClickAction:(UIButton *)sender {
    
    PhotoController *photo = [PhotoController sharedPhotoWithController:self];
    
    photo.blockImageDone = ^(UIImage *image) {
        [EasyLodingView showLodingText:@"图片上传中..." config:^EasyLodingConfig *{
            return [EasyLodingConfig shared].setLodingType(LodingShowTypeIndicator);
        }];
        
        NSData *data = UIImagePNGRepresentation(image);
        BmobFile *file = [[BmobFile alloc] initWithFileName:@"headImg.png" withFileData:data];
        
    
        
        
//        BmobUser *buser = [BmobUser currentUser];
        [file saveInBackground:^(BOOL isSuccessful, NSError *error) {
            //如果文件保存成功，则把文件添加到filetype列
            if (isSuccessful) {
                
                self.imgUrl = file.url;
                [self.showImg sd_setImageWithURL:[NSURL URLWithString:file.url]];
                
//                //上传文件的URL地址
//                [buser setObject:file.url  forKey:@"headImg"];
//                //此处相当于新建一条记录,         //关联至已有的记录请使用 [obj updateInBackground];
//                [buser updateInBackground];
                [EasyLodingView hidenLoding];
                [EasyTextView showSuccessText:@"上传成功"];
                self.showImg.image = image;
            }else{
                //进行处理
                [EasyLodingView hidenLoding];
                [EasyTextView showErrorText:error.description];
            }
        }];
        
        
        
        
    };
    
    
}


@end
